package testssss;

import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * Yao
 * 基础测试
 */

public class ActivitiCreateTest {
    //生成activiti数据库表
    @Test
    public void testcretaeDdTable(){
        //默认创建方式
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //自定义xml 创建方式
//        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg1.xml");
        RuntimeService runtimeService = processEngine.getRuntimeService();//运行时服务
        RepositoryService repositoryService = processEngine.getRepositoryService();//仓库服务
        TaskService taskService = processEngine.getTaskService();//任务服务
        HistoryService historyService = processEngine.getHistoryService();//历史服务
        ManagementService managementService = processEngine.getManagementService();//对运行时服务进行管理
        String name = processEngine.getName();//获取引擎名称

    }
    /**
     * 部署流程定义
     */
    @Test
    public void testDeployment() {
        //创建流程引擎ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //得到仓库服务RepositoryService示例
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //repositoryService 进行部署
        Deployment deploymen = repositoryService.createDeployment()
                .addClasspathResource("diagram/3.bpmn20.xml")
                //png是有规范的.leave.png/jpg/git/svg 4种
                .addClasspathResource("diagram/3._100.png")
                .name("请假流程")
                .deploy();
        //输出部署信息
        System.out.println("流程部署ID:" + deploymen.getId());
        System.out.println( "流程部署名称:" + deploymen.getName());

    }
    /**
     * ZIP压缩文件上传方式
     */
    @Test
    public void testDeploymentZip() {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("diagram/diagram.zip");
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()
                .addZipInputStream(zipInputStream)
                .deploy();
        System.out.println("流程部署ID:" + deployment.getId());
        System.out.println("流程部署名称:" + deployment.getName());
    }
    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcessInstance() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("_100");
        System.out.println("流程实例ID:" + processInstance.getId());
        System.out.println("流程定义ID:" + processInstance.getProcessDefinitionId());
        System.out.println("流程定义名称:" + processInstance.getProcessDefinitionName());
    }
    /**
     * 查询当前个人待执行的任务
     */
    @Test
    public void testFindPersonalTaskList() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //processDefinitionKey 流程key  taskAssignee 任务负责人
        List<Task> list = taskService.createTaskQuery().processDefinitionKey("_100").taskAssignee("worker").list();
        for (Task task : list) {
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
            System.out.println("任务ID:" + task.getId());
             System.out.println("任务负责人:" + task.getAssignee());
             System.out.println("任务名称:" + task.getName());
         }

    }
    /**
     * 完成我的任务
     */
    @Test
    public void testCompleteMyPersonalTask() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //只返回一个值
        Task task = taskService.createTaskQuery().processDefinitionKey("_100").taskAssignee("worker").singleResult();

        //任务ID
        taskService.complete( task.getId());
        System.out.println( "完成任务");
    }
    /**
     * 查询流程定义
     */
    @Test
    public void testQueryProcessDefinition() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        List<ProcessDefinition> list = processDefinitionQuery.processDefinitionKey("_100").orderByProcessDefinitionVersion().desc().list();

        for (org.activiti.engine.repository.ProcessDefinition processDefinition : list) {
            System.out.println("流程定义ID:" + processDefinition.getId());
            System.out.println("流程定义名称:" + processDefinition.getName());
            System.out.println("流程定义key:" + processDefinition.getKey());
            System.out.println("流程定义版本:" + processDefinition.getVersion());
            System.out.println("流程定义部署ID:" + processDefinition.getDeploymentId());
            System.out.println("流程定义描述:" + processDefinition.getDescription());
            System.out.println("流程定义资源名称:" + processDefinition.getResourceName());
            System.out.println(processDefinition.getDiagramResourceName());
            System.out.println("=========================");
        }
    }
    /**
     * 查询流程实例
     */
    @Test
    public void queryProcessInstance() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
        List<ProcessInstance> list = processInstanceQuery.processDefinitionKey("chuchai").list();
        for (ProcessInstance processInstance : list) {
            System.out.println("流程实例ID:" + processInstance.getId());
            System.out.println("流程定义ID:" + processInstance.getProcessDefinitionId());
            System.out.println("流程定义名称:" + processInstance.getProcessDefinitionName());
            System.out.println("流程定义key:" + processInstance.getProcessDefinitionKey());
            System.out.println("流程定义版本:" + processInstance.getProcessDefinitionVersion());
            System.out.println("流程定义部署ID:" + processInstance.getDeploymentId());
            System.out.println("是否暂停 "+ processInstance.isSuspended());
            System.out.println("是否执行完成 "+ processInstance.isEnded());
            System.out.println(processInstance.getBusinessKey());
            System.out.println("=====================>");


        }
    }/**
     * 删除流程实例
     */
    @Test
    public void deleteProcessInstance() {
        //流程定义ID
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //运行时服务
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //删除流程实例,如果运行时服务中存在正在执行的任务,则无法删除
//        repositoryService.deleteDeployment("1");
        //设置true 删除流程实例时会级联删除正在执行任务
        repositoryService.deleteDeployment("40001", true);
    }
    /**
     * 查询bpmn信息 通过流程ID
     */
    @Test
    public void queryBpmnInfo() throws IOException {
        //得到流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //流程定义服务
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //查询流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("_100").singleResult();
        System.out.println("流程定义ID:" + processDefinition.getId());
        //流程定义ID
        String deploymentId = processDefinition.getDeploymentId();
        //获取流程定义的图片
        InputStream resourceAsStream = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());
        //获取流程定义的xml文件流
        InputStream resourceAsStream1 = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());
        //创建文件
        File file = new File("D:/activiti.png");
        File file1 = new File("D:/activiti.bpmn");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        FileOutputStream fileOutputStream1 = new FileOutputStream(file1);
        //拷贝文件
        IOUtils.copy(resourceAsStream, fileOutputStream);
        IOUtils.copy(resourceAsStream1, fileOutputStream1);
        fileOutputStream.close();
    }
    /**
     * 查询历史信息 通过流程ID 查询
     */
    @Test
    public void testHistoric01() {
        //获取流程引擎
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //历史服务
        HistoryService historyService = processEngine.getHistoryService();
        //查询历史活动实例
        HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
        //流程实例ID
        historicActivityInstanceQuery.processInstanceId("2501");
        // 按开始时间升序
        historicActivityInstanceQuery.orderByHistoricActivityInstanceStartTime().asc();
        //查询
        List<HistoricActivityInstance> list = historicActivityInstanceQuery.list();
        for (HistoricActivityInstance historicActivityInstance : list) {
            //活动实例ID
            System.out.println(historicActivityInstance.getActivityId());
            //活动实例名称
            System.out.println( historicActivityInstance.getActivityName());
            //活动实例类型
            System.out.println( historicActivityInstance.getProcessDefinitionId());
            //流程定义ID
            System.out.println( historicActivityInstance.getProcessInstanceId());
            System.out.println("======================>");
//            System.out.println( "流程定义ID:" + historicActivityInstance.getProcessDefinitionId());
//            System.out.println( "流程实例ID:" + historicActivityInstance.getProcessInstanceId());
//            System.out.println( "任务ID:" + historicActivityInstance.getTaskId());
//            System.out.println( "任务名称:" + historicActivityInstance.getActivityName());
//            System.out.println( "任务类型:" + historicActivityInstance.getActivityType());
        }




    }




}
